import type { RouteRecordRaw } from 'vue-router'

/**
 * 格式化 tag 类型
 */
export const formatTagType = (status: number) => {
  switch (status) {
    case 0:
      return 'warning'
    case 1:
      return 'success'
    case 2:
      return 'info'
    case 3:
      return 'danger'
  }
}
/**
 * 格式化租赁合同的状态
 */
export const formatRentStatus = (type: number) => {
  switch (type) {
    case 0:
      return '待生效'
    case 1:
      return '生效中'
    case 2:
      return '已到期'
    case 3:
      return '已退租'
  }
}
/**
 * 动态添加属性
 */
export const addKey = (treeList: any[], key: string) => {
  treeList.forEach((item) => {
    item[key] = true
    // 递归处理
    if (item.children) {
      addKey(item.children, key)
    }
  })
}
/**
 * 动态移除属性
 */
export const delKey = (treeList: any[], key: string) => {
  treeList.forEach((item) => {
    delete item[key]

    if (item.children) {
      delKey(item.children, key)
    }
  })
}
/**
 * 处理一级路由权限数据
 */
export const getFirstRoutePerms = (perms: string[]) => {
  const newArr = perms.map((item) => {
    return item.split(':')[0]
  })
  return [...new Set(newArr)]
}
/**
 * 处理二级路由权限数据
 */
export const getSecondRoutePerms = (perms: string[]) => {
  const newArr = perms.map((item) => {
    const arr = item.split(':')
    return arr[0] + ':' + arr[1]
  })

  return [...new Set(newArr)]
}
/**
 * 根据权限点过滤路由表
 */
export const getRoutes = (
  firstRoutePerms: string[],
  secondRoutePerms: string[],
  asyncRoutes: RouteRecordRaw[],
): RouteRecordRaw[] => {
  if (firstRoutePerms[0] === '*' && secondRoutePerms[0] === '*:*') {
    // 超级管理员，直接返回所有动态路由
    return asyncRoutes
  }

  // 过滤一级路由
  const firstRoutes = asyncRoutes.filter((route) => {
    return firstRoutePerms.includes(route.meta!.permission!)
  })
  // 过滤二级路由
  const lastRoutes = firstRoutes.map((route) => {
    const children = route.children?.filter((item) => {
      return secondRoutePerms.includes(item.meta!.permission!)
    })

    return {
      ...route,
      children,
    } as RouteRecordRaw
  })

  // 返回最终路由信息
  return lastRoutes
}
/**
 * 告警记录的处置状态
 */
export const formatHandleStatus = (status: number) => {
  switch (status) {
    case 0:
      return '未派单'
    case 1:
      return '已派单'
    case 2:
      return '已接单'
    case 3:
      return '已完成'
  }
}
